package com.rules.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rules.admin.dao.BusinessModelAttributeDao;
import com.rules.admin.entity.BusinessModelAttributeEntity;
import com.rules.admin.service.BusinessModelAttributeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusinessModelAttributeServiceImpl extends ServiceImpl<BusinessModelAttributeDao, BusinessModelAttributeEntity> implements BusinessModelAttributeService {


   public BusinessModelAttributeEntity insert(BusinessModelAttributeEntity businessModelAttributeEntity) {
      this.save(businessModelAttributeEntity);
      return businessModelAttributeEntity;
   }

   public BusinessModelAttributeEntity update(BusinessModelAttributeEntity businessModelAttributeEntity) {
      this.updateById(businessModelAttributeEntity);
      return businessModelAttributeEntity;
   }



   public void deleteByBusinessModel(long businessModelId) {
      LambdaQueryWrapper<BusinessModelAttributeEntity> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(BusinessModelAttributeEntity::getBusinessModelId, businessModelId);
      this.remove(queryWrapper);
   }

   public BusinessModelAttributeEntity get(long id) {
      BusinessModelAttributeEntity businessModelAttributeEntity = (BusinessModelAttributeEntity)this.get(id);
      businessModelAttributeEntity.setValueExpress(businessModelAttributeEntity.getValueExpress().replace("Integer|", "").replace("params[0].", ""));
      return businessModelAttributeEntity;
   }

   public IPage<BusinessModelAttributeEntity> queryPage(long businessModelId, String name, IPage<BusinessModelAttributeEntity> page) {

      LambdaQueryWrapper<BusinessModelAttributeEntity> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(BusinessModelAttributeEntity::getBusinessModelId, businessModelId);
      if (StringUtils.isNotBlank(name)) {
         queryWrapper.like(BusinessModelAttributeEntity::getAttrName, "%" + name + "%");
      }
      return this.page(page, queryWrapper);
   }

   public List<BusinessModelAttributeEntity> getModelAttributesByModelId(long modelId) {
      LambdaQueryWrapper<BusinessModelAttributeEntity> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(BusinessModelAttributeEntity::getBusinessModelId, modelId);
      return this.baseMapper.selectList(queryWrapper);
   }

   public List<BusinessModelAttributeEntity> getModelAttributesByParentId(long id) {
      LambdaQueryWrapper<BusinessModelAttributeEntity> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(BusinessModelAttributeEntity::getParentId, id);
      return this.baseMapper.selectList(queryWrapper);
   }

   public List<BusinessModelAttributeEntity> getModelAttributes(long modelId, String attrName) {
      LambdaQueryWrapper<BusinessModelAttributeEntity> queryWrapper = new LambdaQueryWrapper<>();
      queryWrapper.eq(BusinessModelAttributeEntity::getBusinessModelId, modelId);
      if (StringUtils.isNotBlank(attrName)) {
         queryWrapper.like(BusinessModelAttributeEntity::getAttrName, "%" + attrName + "%");
      }
      queryWrapper.orderByAsc(BusinessModelAttributeEntity::getAttrName);
      return this.baseMapper.selectList(queryWrapper);

   }
}
